<?php
require_once(APPPATH."models/base.php");

class link_model extends base_model
{	
	function get_class()
	{
		$link_class = $this->link_class_manager->get($this->class_id);
		return $link_class;
	
	}
	function title($len)
	{
		if(strlen($this->title,'UTF-8')<=$len)
		{
			return $this->title;
		}
		else
		{
			return mb_substr($this->title, 0,$len)."..." ;
		}
	}
}

class link_manager extends base_manager
{
	public function get($params ,$load_type = Load_Type::Lazy)
	{
		$link = parent::get($params);
		return $link;
	}

	public function get_list($where = Array()) 
	{
		/*
		$query_result = new stdClass();
		$page = array_remove($where,'page');$page == null && $page = 1 ;
		$page_size = array_remove($where,'page_size');
		$order_by = array_remove($where,'order_by');
		
		$t_link = _T("link");
		
		$where_string = "where 1=1 ";
		$where_string .= $this->build_where_string($where);
		
		$sql_pattern =
			@"select %s from $t_link link %s";
		
		$count_sql = sprintf($sql_pattern,"count(*) as row_number",$where_string);
		$query_result->record_count = $this->db->query($count_sql)->row()->row_number;
		
		if($order_by != null)
		{
			$sql_pattern .= " order by $order_by";
		}
		else 
		{
			$sql_pattern .= " order by id desc"; 
		}
		
		if($page_size != null)
		{
			$begin_index = ($page-1)*$page_size;
			$query_result->page_count = intval($query_result->record_count/$page_size)-1;
			$sql_pattern .= " limit $begin_index,$page_size";
		}
		
		$result_sql = sprintf(
			$sql_pattern,
			@"link.*",
			$where_string
		);
		
		$this->query_result = $query_result;
		return $this->convert_list(
				$this->db->query($result_sql)->result()
		);
		*/
		 
		$class_alias = array_remove($where, 'class.alias');
		$class_id = array_remove($where, 'class.id');
		$descendant = array_remove($where, 'descendant');
		//echo $class_alias;exit;
		$link_class = null;
		if($class_id != null)
		{
			$link_class = $this->link_class_manager->get($class_id);
		}
		if($class_alias != null)
		{
			$link_class = $this->link_class_manager->get(array('alias'=>$class_alias));
		}
		if($link_class!=null)  // 类ID不为空的情况
		{
			$descendant == null && $descendant = 'false';
		
			if($descendant == "false")
			{
				$where['class.id'] = $link_class->id;
			}
			else
			{
				$ids = Array($link_class->id);
				// print_r($ids);
				$descendant = $link_class->descendant;
				if(!function_exists('link_cube'))
				{
					function link_cube($v)
					{
						return $v->id;
					}
				}
				$ids = array_merge($ids,array_map('link_cube',$descendant));
				// print_r($ids);exit;
				$ids = trim(implode(",", $ids),',');
				$where['class.id'] = "in(".$ids.")";
			}
		}
		$query_result = new stdClass();
		$page = array_remove($where,'page');$page == null && $page = 1 ;
		$page_size = array_remove($where,'page_size');
		
		$order_by = array_remove($where,'order_by');
		
		$t_link = _T("link");$t_link_class = _T("link_class");
		
		
		$where_string = "where 1=1 ";
		
		$where_string .= $this->build_where_string($where);
		
		$sql_pattern =
		@"select %s from $t_link link
		left join $t_link_class class on link.class_id=class.id %s";
		
		$count_sql = sprintf($sql_pattern,"count(*) as row_number",$where_string);  
		// echo $count_sql;exit ; 
		
		$query_result->record_count = $this->db->query($count_sql)->row()->row_number;
		
		if($order_by != null)
		{
			$sql_pattern .= " order by $order_by";
		}
		else
		{
			$sql_pattern .= " order by id desc";
		}
		// var_dump($page_size);exit;
		if($page_size != null)
		{
			$begin_index = ($page-1)*$page_size;
			$query_result->page_count = intval($query_result->record_count/$page_size)-1;
			$sql_pattern .= " limit $begin_index,$page_size";
		}
		
		$result_sql = sprintf(
				$sql_pattern,
				@"link.*",
				$where_string
		);
		
		//echo $result_sql;exit;
		$this->query_result = $query_result;
		return $this->convert_list(
				$this->db->query($result_sql)->result()
		);
	} 
	public function moveto($class_id,$link_id) 
	{ 
		$this->update(array("class_id"=>$class_id), array("id"=>$link_id)) ;
	}  
}

?>